package com.sciencep.pro.admin.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sciencep.pro.admin.system.common.util.UserUtil;
import com.sciencep.pro.common.domain.QueryRequest;
import com.sciencep.pro.common.domain.ResponseResult;
import com.sciencep.pro.common.exception.CustomException;
import com.sciencep.pro.core.domain.dto.branch.BranchTO;
import com.sciencep.pro.core.domain.dto.user.UserDetail;
import com.sciencep.pro.core.domain.entity.Branch;
import com.sciencep.pro.core.domain.vo.branch.BranchVO;
import com.sciencep.pro.core.service.IBranchContactService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value = "/branch")
public class BranchController {

    @Autowired
    private IBranchContactService branchContactService;

    @Autowired
    private UserUtil userUtil;

    @GetMapping(value = "/getBranchList")
    @ApiOperation(value = "查询分社及分社成员")
    public ResponseResult<Page<BranchTO>> getBranchList(QueryRequest queryRequest){
        Page<BranchTO> resultList = branchContactService.getBranchList(queryRequest);
        return new ResponseResult<Page<BranchTO>>().success(resultList);
    }

    @PostMapping(value = "/addBranch")
    @ApiOperation(value = "新增分社")
    public ResponseResult<Branch> addBranch(@RequestBody BranchVO branchVO) throws CustomException {
        UserDetail userDetail = userUtil.getCurrentUserDetail();
        return branchContactService.addBranch(branchVO,userDetail);
    }

    @DeleteMapping(value = "/deleteBranch")
    @ApiOperation(value = "删除分社")
    public ResponseResult<Branch> deleteBranch(String id) throws CustomException {
        UserDetail userDetail = userUtil.getCurrentUserDetail();
        return branchContactService.deleteBranch(id,userDetail);
    }

    @PostMapping(value = "/updateBranch")
    @ApiOperation(value = "编辑分社")
    public ResponseResult<Branch> updateBranch(@RequestBody BranchVO branchVO) throws CustomException {
        UserDetail userDetail = userUtil.getCurrentUserDetail();
        return branchContactService.updateBranch(branchVO,userDetail);
    }

}
